/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import core.ChangesLogger;
import core.Database;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Sakis Vtdk <sakis.vtdk@gmail.com>
 */
public class SqlExportPanel extends javax.swing.JPanel {

	/**
	 * Creates new form SqlExportPanel
	 */
	public SqlExportPanel() {
		initComponents();

		//populate the comboBoxes
		try
		{
			populateComboBoxes();
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, "An error occured! Please try again", "Error", JOptionPane.ERROR_MESSAGE);
			log.error("An error occured while initializing SqlExportPanel. The error was: "+ ex.getMessage());
			this.setVisible(false);
		}
	}

	/**
	 * Populates the ComboBoxes
	 */
	private void populateComboBoxes() throws ArrayIndexOutOfBoundsException
	{
		baseVersionComboBox.removeAllItems();
		targetVersionComboBox.removeAllItems();
		
		ArrayList<BigDecimal> targetVersions = ChangesLogger.getChangesLogger().getTargetVersionsList();

		if(targetVersions.isEmpty())
		{
			throw new ArrayIndexOutOfBoundsException("No versions were found");
		}
		else
		{
			//get the versions in reverse order
			for(int i = targetVersions.size()-1; i >= 0; i--)
			{
				String targetVersion = targetVersions.get(i).toString();
				targetVersionComboBox.addItem(targetVersion);

				//The base version for each change, was the targetVersion-versionStep
				BigDecimal baseVersion = targetVersions.get(i).subtract(Database.getVersionStep());

				//if it is a version before the starting version
				if(baseVersion.compareTo(Database.getStartingVersion()) < 0)
				{
					baseVersionComboBox.addItem(this._emptyDatabaseVersion);
				}
				else
				{
					baseVersionComboBox.addItem(baseVersion.toString());
				}
			}
		}
	}

	/**
	 * Check if the user wants to export only the last changes
	 * 
	 * @return True if the corresponding checkbox is selected, false otherwise
	 */
	public boolean exportLastChangesOnly()
	{
		return this.lastChangesCheckBox.isSelected();
	}

	/**
	 * Check if the user wants to export an SQL script to go to previous versions
	 * 
	 * @return True if the corresponding checkbox is selected, false otherwise
	 */
	public boolean generateReverseSql()
	{
		return this.reverseChangesCheckBox.isSelected();
	}

	/**
	 * Gets the currently selected item in the baseVersionComboBox
	 * 
	 * @return The selected version
	 */
	public BigDecimal getSelectedBaseVersion()
	{
		String selectedVersion = (String) baseVersionComboBox.getSelectedItem();
		if(selectedVersion.equals(this._emptyDatabaseVersion))
		{
			return new BigDecimal("0.9");
		}
		else
		{
			return new BigDecimal(selectedVersion);
		}
	}

	/**
	 * Gets the currently selected item in the targetVersionComboBox
	 * 
	 * @return The selected version
	 */
	public BigDecimal getSelectedTargetVersion()
	{
		String selectedVersion = (String) targetVersionComboBox.getSelectedItem();

		return new BigDecimal(selectedVersion);
	}

	private String _emptyDatabaseVersion = "Empty Database";

	private static Logger log = Logger.getLogger(SqlExportPanel.class);

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        baseVersionLabel = new javax.swing.JLabel();
        targetVersionLabel = new javax.swing.JLabel();
        baseVersionComboBox = new javax.swing.JComboBox();
        targetVersionComboBox = new javax.swing.JComboBox();
        lastChangesCheckBox = new javax.swing.JCheckBox();
        reverseChangesCheckBox = new javax.swing.JCheckBox();

        baseVersionLabel.setText("You are in version");

        targetVersionLabel.setText("You want to go to version");

        baseVersionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        targetVersionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lastChangesCheckBox.setText("Export the last set of changes");
        lastChangesCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lastChangesCheckBoxActionPerformed(evt);
            }
        });

        reverseChangesCheckBox.setText("Export reverse script");
        reverseChangesCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reverseChangesCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(reverseChangesCheckBox)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lastChangesCheckBox)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(baseVersionLabel)
                            .addComponent(baseVersionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(targetVersionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(targetVersionComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(131, 131, 131))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(baseVersionLabel)
                    .addComponent(targetVersionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(baseVersionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(targetVersionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(lastChangesCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(reverseChangesCheckBox)
                .addContainerGap(59, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lastChangesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lastChangesCheckBoxActionPerformed
		boolean isSelected = this.lastChangesCheckBox.isSelected();

		this.baseVersionComboBox.setEnabled(!isSelected);
		this.targetVersionComboBox.setEnabled(!isSelected);
		this.reverseChangesCheckBox.setEnabled(!isSelected);
    }//GEN-LAST:event_lastChangesCheckBoxActionPerformed

    private void reverseChangesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reverseChangesCheckBoxActionPerformed
		String tmpLabel = this.targetVersionLabel.getText();
		this.targetVersionLabel.setText(this.baseVersionLabel.getText());
		this.baseVersionLabel.setText(tmpLabel);
    }//GEN-LAST:event_reverseChangesCheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox baseVersionComboBox;
    private javax.swing.JLabel baseVersionLabel;
    private javax.swing.JCheckBox lastChangesCheckBox;
    private javax.swing.JCheckBox reverseChangesCheckBox;
    private javax.swing.JComboBox targetVersionComboBox;
    private javax.swing.JLabel targetVersionLabel;
    // End of variables declaration//GEN-END:variables
}
